package lia.indexing;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.DateTools;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.NumericField;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.RAMDirectory;

import static lia.util.Constants.*;

/**
 * Fragments：Just to test the code compiles
 * 
 * @author F.H Dragon(wenlong.meng@gmail.com)
 * @version 1.0 at 2011/08/07
 * @since 1.0
 */
public class Fragments {
	
	//local variables 
	/**
	 * import domain
	 */
	public static final String COMPANY_DOMAIN = "fhdragon.com";
	/**
	 * block domain
	 */
	public static final String BAD_DOMAIN = "block.com";
	
	/**
	 * add boost into doc
	 */
	public void docBoostMethod(){
		//build dir
		Directory dir = new RAMDirectory();
		//Create indexWriter
		IndexWriterConfig iwc = new IndexWriterConfig(LUCENE_VERSION, new StandardAnalyzer(LUCENE_VERSION));
		try {
			IndexWriter iw = new IndexWriter(dir, iwc);
			Document doc = new Document();
			doc.add(new Field("senderEmail", "bob@smith.com", Field.Store.YES, Field.Index.NOT_ANALYZED));
			doc.add(new Field("senderName", "Bob Smith", Field.Store.YES, Field.Index.ANALYZED));
			doc.add(new Field("senderDomain", COMPANY_DOMAIN, Field.Store.YES, Field.Index.ANALYZED));
			doc.add(new Field("subject", "Hi there Lisa", Field.Store.YES, Field.Index.ANALYZED));
			doc.add(new Field("body", "I don't have much to say", Field.Store.NO, Field.Index.ANALYZED));
			if(isImportant(COMPANY_DOMAIN)){
				doc.setBoost(1.5f);//good domain boost factor:1.5
			}else if(isUnimportant(COMPANY_DOMAIN)){
				doc.setBoost(0.1f);//bad domain boost factor:0.1
			}
			iw.addDocument(doc);
			iw.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * add boost into field
	 */
	public void fieldBoostMethod(){
		String subject = "Hi there Lisa";
		Field subjectField = new Field("subject", subject, Field.Store.YES, Field.Index.ANALYZED);
		subjectField.setBoost(1.2f);
	}
	
	/**
	 * ramDir ...
	 * 
	 * @throws CorruptIndexException
	 * @throws LockObtainFailedException
	 * @throws IOException
	 */
	public void ramDirExample() throws CorruptIndexException, LockObtainFailedException, IOException{
		Analyzer analyzer = new WhitespaceAnalyzer(LUCENE_VERSION);
		Directory ramDir = new RAMDirectory();
		IndexWriterConfig iwc = new IndexWriterConfig(LUCENE_VERSION, analyzer);
		@SuppressWarnings("unused")
		IndexWriter iw = new IndexWriter(ramDir, iwc);
	}
	
	/**
	 * dir copy
	 * 
	 * @throws IOException
	 */
	public void dirCopy() throws IOException{
		Directory otherDir = null;
		@SuppressWarnings("unused")
		Directory ramDir = new RAMDirectory(otherDir);
	}
	
	/**
	 * index without optimize
	 * 
	 * @throws CorruptIndexException
	 * @throws LockObtainFailedException
	 * @throws IOException
	 */
	@SuppressWarnings("deprecation")
	public void addIndexs() throws CorruptIndexException, LockObtainFailedException, IOException{
		Directory otherDir = null;
		Directory ramDir = null;
		Analyzer analyzer = null;
		
		IndexWriterConfig iwc = new IndexWriterConfig(LUCENE_VERSION, analyzer);
		IndexWriter iw = new IndexWriter(otherDir, iwc);
		
		iw.addIndexesNoOptimize(new Directory[]{ramDir});
	}
	
	/**
	 * indexNumberMethod
	 */
	public static void indexNumberMethod(){
		new Field("size", "4096", Field.Store.YES, Field.Index.NOT_ANALYZED);
		new Field("price", "10.99", Field.Store.YES, Field.Index.NOT_ANALYZED);
		new Field("author", "Arthur C. Clark", Field.Store.YES, Field.Index.NOT_ANALYZED);
	}
	
	/**
	 * number field
	 */
	public void numberField(){
		Document doc = new Document();
		doc.add(new NumericField("price").setDoubleValue(19.99));
	}
	
	/**
	 * index timestamp with number
	 */
	public void numberTimestamp(){
		Document doc = new Document();
		doc.add(new NumericField("timestamp").setLongValue(new Date().getTime()));
		doc.add(new NumericField("day").setIntValue((int)(new Date().getTime()/24/3600)));
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		doc.add(new NumericField("dayOfMonth").setIntValue(cal.get(Calendar.DAY_OF_MONTH)));
	}
	
	/**
	 * check important domain
	 * 
	 * @param domain
	 * @return
	 */
	public boolean isImportant(String domain){
		return domain.toLowerCase().endsWith(COMPANY_DOMAIN);
	}
	
	/**
	 * check unimportant domain
	 * 
	 * @param domain
	 * @return
	 */
	public boolean isUnimportant(String domain){
		return domain.toLowerCase().endsWith(BAD_DOMAIN);
	}
	
	/**
	 * set infoStream
	 * 
	 * @throws CorruptIndexException
	 * @throws LockObtainFailedException
	 * @throws IOException
	 */
	public void setInfoStream() throws CorruptIndexException, LockObtainFailedException, IOException{
		Directory dir = null;
		Analyzer analyzer = null;
		IndexWriterConfig iwc = new IndexWriterConfig(LUCENE_VERSION, analyzer);
		IndexWriter iw = new IndexWriter(dir, iwc);
		
		iw.setInfoStream(System.out);
	}
	
	/**
	 * index  date 
	 */
	public void dateMethod() {
		Document doc = new Document();
		doc.add(new Field("indexDate", DateTools.dateToString(new Date(),
				DateTools.Resolution.DAY), Field.Store.YES,
				Field.Index.NOT_ANALYZED));
	}
	
	/**
	 * numeric field
	 */
	public void numericField(){
		Document doc = new Document();
		NumericField price = new NumericField("price").setDoubleValue(19.99);
		doc.add(price);
		
		NumericField timestamp = new NumericField("timestamp").setLongValue(new Date().getTime());
		doc.add(timestamp);
		
		Date b = new Date();
		String v = DateTools.dateToString(b, DateTools.Resolution.DAY);
		NumericField birthday = new NumericField("birthday").setIntValue(Integer.parseInt(v));
		doc.add(birthday);
	}
	
	/**
	 * index authors
	 */
	public void indexAuthors(){
		String[] authors = new String[]{"lisa","tom"};
		Document doc = new Document();
		for(String author : authors){
			doc.add(new Field("author", author, Field.Store.YES, Field.Index.ANALYZED));
		}
	}
	
}
